package com.hxzy.shop.controller.commodity;

import com.github.pagehelper.PageInfo;
import com.hxzy.shop.common.dto.commodity.req.ClassificationsReq;
import com.hxzy.shop.common.dto.commodity.resp.ClassifiedManagementResp;
import com.hxzy.shop.feign.commodity.CommodityFeign;
import com.hxzy.shop.vo.ApiException;
import com.hxzy.shop.vo.Result;
import com.hxzy.shop.vo.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.hxzy.shop.dto.commodity.req.*;
import io.swagger.annotations.ApiParam;

import javax.validation.Valid;
import java.util.List;

/**
 * 功能描述: 商品分类管理
 *
 * @author tonneyyy
 */
@RestController
@Api(tags = "分类管理")
@RequestMapping("/classification")
public class ClassificationsController {


    /**
     * 商品相关的feign模块远程调用 (commodity项目)
     */
    @Autowired
    private CommodityFeign commodityFeign;

    /**
     * 获得分类列表分页信息
     * @param req 分页封装请求类
     * @return 返回分页列表
     */
    @ApiOperation("获得分类管理分页")
    @GetMapping("/getClassifiedPage")
    public Result<PageInfo<ClassifiedManagementResp>> getClassifiedPage(@Valid ClassificationsReq  req) {
        Result<PageInfo<ClassifiedManagementResp>> classifiedPageInfo = commodityFeign.getClassifiedPage(req);
        return classifiedPageInfo;
    }

    /**
     * 获得分类列表
     * @return 分类列表
     */
    @ApiOperation("获得分类列表")
    @PostMapping("/getClassifiedList")
    public Result<List<ClassifiedManagementResp>> getClassifiedList() {
        Result<List<ClassifiedManagementResp>> classifiedList = commodityFeign.getClassifiedList();
        if (classifiedList.getCode() != ResultCode.SUCCESS.getCode()) {
            throw new ApiException(500, classifiedList.getMsg());
        }
        return Result.success(classifiedList.getData());
    }
    /********************新***********************/
    @ApiOperation("获得分类列表")
    @PostMapping("/getClassifiedManagementList")
    public Result<List<ClassifiedManagementResp>> getClassifiedManagementList(){
        return commodityFeign.getClassifiedManagement();
    }

     /**
     * 添加分类
     * @param addClassifiedReq 添加分类请求类
     * @return 是否添加成功
     */
    @ApiOperation("添加分类")
    @PostMapping("/addClassified")
    public Result<Boolean> addClassified(@ApiParam(required = true, name = "addClassifiedVo", value = "添加分类的model")
                                         @RequestBody @Valid AddClassifiedReq addClassifiedReq) {
        Result<Boolean> booleanResult = commodityFeign.addClassified(addClassifiedReq);
        if (booleanResult.getCode() != ResultCode.SUCCESS.getCode()) {
            throw new ApiException(500, booleanResult.getMsg());
        }
        return Result.success(booleanResult.getData());
    }
    /**
     * 根据id 获得分类详细信息
     * @param id 分类id
     * @return 返回分类信息
     */
    @PostMapping("/getClassified/{id}")
    @ApiOperation("根据id获得分类详细信息")
    public Result<ClassifiedManagementResp> getClassified(@ApiParam(required = true, name = "id", value = "分类的id")
                                                          @PathVariable("id") Long id) {
        Result<ClassifiedManagementResp> classified = commodityFeign.getClassified(id);
        if (classified.getCode() != ResultCode.SUCCESS.getCode()) {
            throw new ApiException(500, classified.getMsg());
        }
        return Result.success(classified.getData());
    }
    /**
     * 更新分类
     * @param addClassifiedReq 更新分类实体类
     * @return 是否更新成功
     */
    @ApiOperation("更新分类")
    @PostMapping("/updateClassified")
    public Result<Boolean> updateClassified(@ApiParam(required = true, name = "addClassifiedVo", value = "更新分类请求model")
                                            @RequestBody AddClassifiedReq addClassifiedReq) {
        Result<Boolean> booleanResult = commodityFeign.updateClassified(addClassifiedReq);
        if (booleanResult.getCode() != ResultCode.SUCCESS.getCode()) {
            throw new ApiException(500, booleanResult.getMsg());
        }
        return Result.success(booleanResult.getData());
    }
    /**
     * 根据id 删除指定分类
     * @param id 需要删除分类的id
     * @return 是否删除成功
     */
    @PostMapping("/deleteClassified/{id}")
    @ApiOperation("根据id 删除指定的分类")
    public Result<Boolean> deleteClassified(@ApiParam(required = true, name = "id", value = "需要删除分类的id")
                                            @PathVariable("id") Long id) {
        Result<Boolean> booleanResult = commodityFeign.deleteClassified(id);
        if (booleanResult.getCode() != ResultCode.SUCCESS.getCode()) {
            throw new ApiException(500, booleanResult.getMsg());
        }
        return Result.success(booleanResult.getData());
    }


    /**
     * 更新状态
     * @param state 状态
     * @return 是否成功
     */
    @ApiOperation("更新状态")
    @PostMapping("/updateState")
    public Result<Boolean> updateState(@ApiParam(readOnly = true, name = "state", value = "状态")
                                       @RequestBody StateUpdateRep state) {
        Result<Boolean> booleanResult = commodityFeign.updateState(state);
        if (booleanResult.getCode() != ResultCode.SUCCESS.getCode()) {
            throw new ApiException(500, booleanResult.getMsg());
        }
        return Result.success(booleanResult.getData());
    }



    @ApiOperation("新增分类")
    @PostMapping("/addNewClassified")
    public Result<Object> addNewClassified(@RequestBody AddNewClassifiedReq req){

        return commodityFeign.addNewClassified(req);
    }
    @ApiOperation("编辑分类")
    @PostMapping("/editClassified")
    public Result<Object> editClassified(@RequestBody EditClassifiedReq req){

        return commodityFeign.editClassified(req);
    }

    @ApiOperation("删除分类")
    @PostMapping("/deleteClassifiedById")
    public Result<Object> deleteClassifiedById(@RequestBody ClassifiedIdReq req){
        return commodityFeign.deleteClassifiedById(req);
    }
    @ApiOperation("调整分类排序")
    @PostMapping("/changeClassificationSort")
    public Result<Object> changeClassificationSort(@RequestBody ClassificationSortReq req){
        return commodityFeign.changeClassificationSort(req);
    }

}
